<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>selection_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_dom_selection_test.js.html">selection_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.dom.selectionTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.dom.selectionTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.dom&#39;);
<a name="line19"></a>goog.require(&#39;goog.dom.selection&#39;);
<a name="line20"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line21"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line22"></a>
<a name="line23"></a>var input;
<a name="line24"></a>var hiddenInput;
<a name="line25"></a>var textarea;
<a name="line26"></a>var hiddenTextarea;
<a name="line27"></a>
<a name="line28"></a>function setUp() {
<a name="line29"></a>  input = goog.dom.createDom(&#39;input&#39;, {type: &#39;text&#39;});
<a name="line30"></a>  textarea = goog.dom.createDom(&#39;textarea&#39;);
<a name="line31"></a>  hiddenInput = goog.dom.createDom(
<a name="line32"></a>      &#39;input&#39;, {type: &#39;text&#39;, style: &#39;display: none&#39;});
<a name="line33"></a>  hiddenTextarea = goog.dom.createDom(
<a name="line34"></a>      &#39;textarea&#39;, {style: &#39;display: none&#39;});
<a name="line35"></a>
<a name="line36"></a>  document.body.appendChild(input);
<a name="line37"></a>  document.body.appendChild(textarea);
<a name="line38"></a>  document.body.appendChild(hiddenInput);
<a name="line39"></a>  document.body.appendChild(hiddenTextarea);
<a name="line40"></a>}
<a name="line41"></a>
<a name="line42"></a>function tearDown() {
<a name="line43"></a>  goog.dom.removeNode(input);
<a name="line44"></a>  goog.dom.removeNode(textarea);
<a name="line45"></a>  goog.dom.removeNode(hiddenInput);
<a name="line46"></a>  goog.dom.removeNode(hiddenTextarea);
<a name="line47"></a>}
<a name="line48"></a>
<a name="line49"></a>
<a name="line50"></a>/**
<a name="line51"></a> * Tests getStart routine in both input and textarea.
<a name="line52"></a> */
<a name="line53"></a>function testGetStartInput() {
<a name="line54"></a>  getStartHelper(input, hiddenInput);
<a name="line55"></a>}
<a name="line56"></a>
<a name="line57"></a>function testGetStartTextarea() {
<a name="line58"></a>  getStartHelper(textarea, hiddenTextarea);
<a name="line59"></a>}
<a name="line60"></a>
<a name="line61"></a>function getStartHelper(field, hiddenField) {
<a name="line62"></a>  assertEquals(0, goog.dom.selection.getStart(field));
<a name="line63"></a>  assertEquals(0, goog.dom.selection.getStart(hiddenField));
<a name="line64"></a>
<a name="line65"></a>  field.focus();
<a name="line66"></a>  assertEquals(0, goog.dom.selection.getStart(field));
<a name="line67"></a>}
<a name="line68"></a>
<a name="line69"></a>
<a name="line70"></a>/**
<a name="line71"></a> * Tests the setText routine for both input and textarea
<a name="line72"></a> * with a single line of text.
<a name="line73"></a> */
<a name="line74"></a>function testSetTextInput() {
<a name="line75"></a>  setTextHelper(input);
<a name="line76"></a>}
<a name="line77"></a>
<a name="line78"></a>function testSetTextTextarea() {
<a name="line79"></a>  setTextHelper(textarea);
<a name="line80"></a>}
<a name="line81"></a>
<a name="line82"></a>function setTextHelper(field) {
<a name="line83"></a>  // Test one line string only
<a name="line84"></a>  select(field);
<a name="line85"></a>  assertEquals(&#39;&#39;, goog.dom.selection.getText(field));
<a name="line86"></a>
<a name="line87"></a>  goog.dom.selection.setText(field, &#39;Get Behind Me Satan&#39;);
<a name="line88"></a>  assertEquals(&#39;Get Behind Me Satan&#39;, goog.dom.selection.getText(field));
<a name="line89"></a>}
<a name="line90"></a>
<a name="line91"></a>
<a name="line92"></a>/**
<a name="line93"></a> * Tests the setText routine for textarea with multiple lines of text.
<a name="line94"></a> */
<a name="line95"></a>function testSetTextMultipleLines() {
<a name="line96"></a>  select(textarea);
<a name="line97"></a>  assertEquals(&#39;&#39;, goog.dom.selection.getText(textarea));
<a name="line98"></a>  var message = goog.userAgent.IE ?
<a name="line99"></a>                &#39;Get Behind Me\r\nSatan&#39; :
<a name="line100"></a>                &#39;Get Behind Me\nSatan&#39;;
<a name="line101"></a>  goog.dom.selection.setText(textarea, message);
<a name="line102"></a>  assertEquals(message, goog.dom.selection.getText(textarea));
<a name="line103"></a>
<a name="line104"></a>  // Select the text upto the point just after the \r\n combination
<a name="line105"></a>  // or \n in GECKO.
<a name="line106"></a>  var endOfNewline = goog.userAgent.IE ? 15 : 14;
<a name="line107"></a>  var selectedMessage = message.substring(0, endOfNewline);
<a name="line108"></a>  goog.dom.selection.setStart(textarea, 0);
<a name="line109"></a>  goog.dom.selection.setEnd(textarea, endOfNewline);
<a name="line110"></a>  assertEquals(selectedMessage, goog.dom.selection.getText(textarea));
<a name="line111"></a>
<a name="line112"></a>  selectedMessage = goog.userAgent.IE ? &#39;\r\n&#39; : &#39;\n&#39;;
<a name="line113"></a>  goog.dom.selection.setStart(textarea, 13);
<a name="line114"></a>  goog.dom.selection.setEnd(textarea, endOfNewline);
<a name="line115"></a>  assertEquals(selectedMessage, goog.dom.selection.getText(textarea));
<a name="line116"></a>}
<a name="line117"></a>
<a name="line118"></a>
<a name="line119"></a>/**
<a name="line120"></a> * Tests the setCursor routine for both input and textarea.
<a name="line121"></a> */
<a name="line122"></a>function testSetCursorInput() {
<a name="line123"></a>  setCursorHelper(input);
<a name="line124"></a>}
<a name="line125"></a>
<a name="line126"></a>function testSetCursorTextarea() {
<a name="line127"></a>  setCursorHelper(textarea);
<a name="line128"></a>}
<a name="line129"></a>
<a name="line130"></a>function setCursorHelper(field) {
<a name="line131"></a>  select(field);
<a name="line132"></a>  // try to set the cursor beyond the length of the content
<a name="line133"></a>  goog.dom.selection.setStart(field, 5);
<a name="line134"></a>  goog.dom.selection.setEnd(field, 15);
<a name="line135"></a>  assertEquals(0, goog.dom.selection.getStart(field));
<a name="line136"></a>  assertEquals(0, goog.dom.selection.getEnd(field));
<a name="line137"></a>
<a name="line138"></a>  select(field);
<a name="line139"></a>  var message = &#39;Get Behind Me Satan&#39;;
<a name="line140"></a>  goog.dom.selection.setText(field, message);
<a name="line141"></a>  goog.dom.selection.setStart(field, 5);
<a name="line142"></a>  goog.dom.selection.setEnd(field, message.length);
<a name="line143"></a>  assertEquals(5, goog.dom.selection.getStart(field));
<a name="line144"></a>  assertEquals(message.length, goog.dom.selection.getEnd(field));
<a name="line145"></a>
<a name="line146"></a>  // Set the end before the start, and see if getEnd returns the start
<a name="line147"></a>  // position itself.
<a name="line148"></a>  goog.dom.selection.setStart(field, 5);
<a name="line149"></a>  goog.dom.selection.setEnd(field, 3);
<a name="line150"></a>  assertEquals(3, goog.dom.selection.getEnd(field));
<a name="line151"></a>}
<a name="line152"></a>
<a name="line153"></a>
<a name="line154"></a>/**
<a name="line155"></a> * Tests the getText and setText routines acting on selected text in
<a name="line156"></a> * both input and textarea.
<a name="line157"></a> */
<a name="line158"></a>function testGetAndSetSelectedTextInput() {
<a name="line159"></a>  getAndSetSelectedTextHelper(input);
<a name="line160"></a>}
<a name="line161"></a>
<a name="line162"></a>function testGetAndSetSelectedTextTextarea() {
<a name="line163"></a>  getAndSetSelectedTextHelper(textarea);
<a name="line164"></a>}
<a name="line165"></a>
<a name="line166"></a>function getAndSetSelectedTextHelper(field) {
<a name="line167"></a>  select(field);
<a name="line168"></a>  goog.dom.selection.setText(field, &#39;Get Behind Me Satan&#39;);
<a name="line169"></a>
<a name="line170"></a>  // select &#39;Behind&#39;
<a name="line171"></a>  goog.dom.selection.setStart(field, 4);
<a name="line172"></a>  goog.dom.selection.setEnd(field, 10);
<a name="line173"></a>  assertEquals(&#39;Behind&#39;, goog.dom.selection.getText(field));
<a name="line174"></a>
<a name="line175"></a>  goog.dom.selection.setText(field, &#39;In Front Of&#39;);
<a name="line176"></a>  goog.dom.selection.setStart(field, 0);
<a name="line177"></a>  goog.dom.selection.setEnd(field, 100);
<a name="line178"></a>  assertEquals(&#39;Get In Front Of Me Satan&#39;, goog.dom.selection.getText(field));
<a name="line179"></a>}
<a name="line180"></a>
<a name="line181"></a>
<a name="line182"></a>/**
<a name="line183"></a> * Test setStart on hidden input and hidden textarea.
<a name="line184"></a> */
<a name="line185"></a>function testSetCursorOnHiddenInput() {
<a name="line186"></a>  setCursorOnHiddenInputHelper(hiddenInput);
<a name="line187"></a>}
<a name="line188"></a>
<a name="line189"></a>function testSetCursorOnHiddenTextarea() {
<a name="line190"></a>  setCursorOnHiddenInputHelper(hiddenTextarea);
<a name="line191"></a>}
<a name="line192"></a>
<a name="line193"></a>function setCursorOnHiddenInputHelper(hiddenField) {
<a name="line194"></a>  goog.dom.selection.setStart(hiddenField, 0);
<a name="line195"></a>  assertEquals(0, goog.dom.selection.getStart(hiddenField));
<a name="line196"></a>}
<a name="line197"></a>
<a name="line198"></a>
<a name="line199"></a>/**
<a name="line200"></a> * Test setStart, setEnd, getStart and getEnd in textarea with text
<a name="line201"></a> * containing line breaks.
<a name="line202"></a> */
<a name="line203"></a>function testSetAndGetCursorWithLineBreaks() {
<a name="line204"></a>  select(textarea);
<a name="line205"></a>  var newline = goog.userAgent.IE ? &#39;\r\n&#39; : &#39;\n&#39;;
<a name="line206"></a>  var message = &#39;Hello&#39; + newline + &#39;World&#39;;
<a name="line207"></a>  goog.dom.selection.setText(textarea, message);
<a name="line208"></a>
<a name="line209"></a>  // Test setEnd and getEnd, by setting the cursor somewhere after the
<a name="line210"></a>  // \r\n combination.
<a name="line211"></a>  goog.dom.selection.setEnd(textarea, 9);
<a name="line212"></a>  assertEquals(9, goog.dom.selection.getEnd(textarea));
<a name="line213"></a>
<a name="line214"></a>  // Test basic setStart and getStart
<a name="line215"></a>  goog.dom.selection.setStart(textarea, 10);
<a name="line216"></a>  assertEquals(10, goog.dom.selection.getStart(textarea));
<a name="line217"></a>
<a name="line218"></a>  // Test setEnd and getEnd, by setting the cursor exactly after the
<a name="line219"></a>  // \r\n combination in IE or after \n in GECKO.
<a name="line220"></a>  var endOfNewline = goog.userAgent.IE ? 7 : 6;
<a name="line221"></a>  checkSetAndGetTextarea(endOfNewline, endOfNewline);
<a name="line222"></a>
<a name="line223"></a>  // Select a \r\n combination in IE or \n in GECKO and see if
<a name="line224"></a>  // getStart and getEnd work correctly.
<a name="line225"></a>  clearField(textarea);
<a name="line226"></a>  message = &#39;Hello&#39; + newline + newline + &#39;World&#39;;
<a name="line227"></a>  goog.dom.selection.setText(textarea, message);
<a name="line228"></a>  var startOfNewline = goog.userAgent.IE ? 7 : 6;
<a name="line229"></a>  endOfNewline = goog.userAgent.IE ? 9 : 7;
<a name="line230"></a>  checkSetAndGetTextarea(startOfNewline, endOfNewline);
<a name="line231"></a>
<a name="line232"></a>  // Select 2 \r\n combinations in IE or 2 \ns in GECKO and see if getStart
<a name="line233"></a>  // and getEnd work correctly.
<a name="line234"></a>  checkSetAndGetTextarea(5, endOfNewline);
<a name="line235"></a>
<a name="line236"></a>  // Position cursor b/w 2 \r\n combinations in IE or 2 \ns in GECKO and see
<a name="line237"></a>  // if getStart and getEnd work correctly.
<a name="line238"></a>  clearField(textarea);
<a name="line239"></a>  message = &#39;Hello&#39; + newline + newline + newline + newline + &#39;World&#39;;
<a name="line240"></a>  goog.dom.selection.setText(textarea, message);
<a name="line241"></a>  var middleOfNewlines = goog.userAgent.IE ? 9 : 7;
<a name="line242"></a>  checkSetAndGetTextarea(middleOfNewlines, middleOfNewlines);
<a name="line243"></a>
<a name="line244"></a>  // Position cursor at end of a textarea which ends with \r\n in IE or \n in
<a name="line245"></a>  // GECKO.
<a name="line246"></a>  clearField(textarea);
<a name="line247"></a>  message = &#39;Hello&#39; + newline + newline;
<a name="line248"></a>  goog.dom.selection.setText(textarea, message);
<a name="line249"></a>  var endOfTextarea = message.length;
<a name="line250"></a>  checkSetAndGetTextarea(endOfTextarea, endOfTextarea);
<a name="line251"></a>
<a name="line252"></a>  // Position cursor at the end of the 2 starting \r\ns in IE or \ns in GECKO
<a name="line253"></a>  // within a textarea.
<a name="line254"></a>  clearField(textarea);
<a name="line255"></a>  message = newline + newline + &#39;World&#39;;
<a name="line256"></a>  goog.dom.selection.setText(textarea, message);
<a name="line257"></a>  var endOfTwoNewlines = goog.userAgent.IE ? 4 : 2;
<a name="line258"></a>  checkSetAndGetTextarea(endOfTwoNewlines, endOfTwoNewlines);
<a name="line259"></a>
<a name="line260"></a>  // Position cursor at the end of the first \r\n in IE or \n in
<a name="line261"></a>  // GECKO within a textarea.
<a name="line262"></a>  endOfOneNewline = goog.userAgent.IE ? 2 : 1;
<a name="line263"></a>  checkSetAndGetTextarea(endOfOneNewline, endOfOneNewline);
<a name="line264"></a>}
<a name="line265"></a>
<a name="line266"></a>
<a name="line267"></a>/**
<a name="line268"></a> * Test to make sure there&#39;s no error when getting the range of an unselected
<a name="line269"></a> * textarea. See bug 1274027.
<a name="line270"></a> */
<a name="line271"></a>function testGetStartOnUnfocusedTextarea() {
<a name="line272"></a>  input.value = &#39;White Blood Cells&#39;;
<a name="line273"></a>  input.focus();
<a name="line274"></a>  goog.dom.selection.setCursorPosition(input, 5);
<a name="line275"></a>
<a name="line276"></a>  assertEquals(&#39;getStart on input should return where we put the cursor&#39;,
<a name="line277"></a>      5, goog.dom.selection.getStart(input));
<a name="line278"></a>
<a name="line279"></a>  assertEquals(&#39;getStart on unfocused textarea should succeed without error&#39;,
<a name="line280"></a>      0, goog.dom.selection.getStart(textarea));
<a name="line281"></a>}
<a name="line282"></a>
<a name="line283"></a>
<a name="line284"></a>/**
<a name="line285"></a> * Test to make sure there&#39;s no error setting cursor position within a
<a name="line286"></a> * textarea after a newline. This is problematic on IE because of the
<a name="line287"></a> * &#39;\r\n&#39; vs &#39;\n&#39; issue.
<a name="line288"></a> */
<a name="line289"></a>function testSetCursorPositionTextareaWithNewlines() {
<a name="line290"></a>  textarea.value = &#39;Hello\nWorld&#39;;
<a name="line291"></a>  textarea.focus();
<a name="line292"></a>
<a name="line293"></a>  // Set the selection point between &#39;W&#39; and &#39;o&#39;. Position is computed this
<a name="line294"></a>  // way instead of being hard-coded because it&#39;s different in IE due to \r\n
<a name="line295"></a>  // vs \n.
<a name="line296"></a>  goog.dom.selection.setCursorPosition(textarea, textarea.value.length - 4);
<a name="line297"></a>
<a name="line298"></a>  var linebreak = goog.userAgent.IE ? &#39;\r\n&#39; : &#39;\n&#39;;
<a name="line299"></a>  var expectedLeftString = &#39;Hello&#39; + linebreak + &#39;W&#39;;
<a name="line300"></a>
<a name="line301"></a>  assertEquals(&#39;getStart on input should return after the newline&#39;,
<a name="line302"></a>      expectedLeftString.length, goog.dom.selection.getStart(textarea));
<a name="line303"></a>  assertEquals(&#39;getEnd on input should return after the newline&#39;,
<a name="line304"></a>      expectedLeftString.length, goog.dom.selection.getEnd(textarea));
<a name="line305"></a>
<a name="line306"></a>  goog.dom.selection.setEnd(textarea, textarea.value.length);
<a name="line307"></a>  assertEquals(&#39;orld&#39;, goog.dom.selection.getText(textarea));
<a name="line308"></a>}
<a name="line309"></a>
<a name="line310"></a>
<a name="line311"></a>/**
<a name="line312"></a> * Helper function to clear the textfield contents.
<a name="line313"></a> */
<a name="line314"></a>function clearField(field) {
<a name="line315"></a>  field.value = &#39;&#39;;
<a name="line316"></a>}
<a name="line317"></a>
<a name="line318"></a>
<a name="line319"></a>/**
<a name="line320"></a> * Helper function to set the start and end and assert the getter values.
<a name="line321"></a> */
<a name="line322"></a>function checkSetAndGetTextarea(start, end) {
<a name="line323"></a>  goog.dom.selection.setStart(textarea, start);
<a name="line324"></a>  goog.dom.selection.setEnd(textarea, end);
<a name="line325"></a>  assertEquals(start, goog.dom.selection.getStart(textarea));
<a name="line326"></a>  assertEquals(end, goog.dom.selection.getEnd(textarea));
<a name="line327"></a>}
<a name="line328"></a>
<a name="line329"></a>
<a name="line330"></a>/**
<a name="line331"></a> * Helper function to focus and select a field. In IE8, selected
<a name="line332"></a> * fields need focus.
<a name="line333"></a> */
<a name="line334"></a>function select(field) {
<a name="line335"></a>  field.focus();
<a name="line336"></a>  field.select();
<a name="line337"></a>}
</pre>


</body>
</html>
